﻿using System.Collections.Generic;
using System.Web.Mvc;
using Journal.Common.Objects.Column;

namespace Journal.Common.Helpers.HtmlControls
{
    public static class TableControl
    {
        public static MvcHtmlString ImageLinkColumn(string value, string url, string imageSrc)
        {
            MvcHtmlString imageLink = LinkControls.ImageLinkControl(value, url, imageSrc);
            return new MvcHtmlString(TableColumn(imageLink.ToString()).ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString ListImageLinksColumn(IEnumerable<ImageLinkColumn> columns)
        {
            TagBuilder column = TableColumn();
            foreach (var item in columns)
            {
                column.InnerHtml += item.Render();
            }

            return new MvcHtmlString(column.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString ListLinksColumn(IEnumerable<LinkColumn> columns)
        {
            TagBuilder column = TableColumn();
            column.AddCssClass("list-links");
            foreach (var item in columns)
            {
                column.InnerHtml += LinkControls.Link(item.Value, item.Url, item.Attributes, item.DataParams);
                if(item.HasSeparator)
                {
                    column.InnerHtml += "|";
                }
            }

            return new MvcHtmlString(column.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString ListControlsColumn(IEnumerable<IBaseColumn> columns)
        {
            TagBuilder column = TableColumn();
            column.AddCssClass("list-controls");
            foreach (var item in columns)
            {
                column.InnerHtml += item.Render();
            }

            return new MvcHtmlString(column.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString StringColumn(string value)
        {
            return new MvcHtmlString(TableColumn(value).ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString ImageColumn(string imageSrc, string altText)
        {
            TagBuilder column = TableColumn();
            column.InnerHtml += ContentControls.Image(imageSrc, altText);
            return new MvcHtmlString(column.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString LinkColumn(string url, string value)
        {
            TagBuilder column = TableColumn();
            column.InnerHtml += LinkControls.LinkControl(value, url);
            return new MvcHtmlString(column.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString LinkColumn(string url, string value, object attributes)
        {
            TagBuilder column = TableColumn();
            column.InnerHtml += LinkControls.LinkControl(value, url, attributes);
            return new MvcHtmlString(column.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString LinkColumn(string url, string value, object attributes, IDictionary<string, string> dataParams)
        {
            TagBuilder column = TableColumn();
            column.InnerHtml += LinkControls.LinkControl(value, url, attributes, dataParams);
            return new MvcHtmlString(column.ToString(TagRenderMode.Normal));
        }

        public static TagBuilder TableColumn(string content)
        {
            TagBuilder column = TableColumn();
            column.InnerHtml += content;
            return column;
        }

        public static TagBuilder TableColumn()
        {
            TagBuilder column = new TagBuilder("td");
            return column;
        }
    }
}